<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="资源名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="资源名称"></el-input>
    </el-form-item>
    <el-form-item label="资源简介" prop="summary">
      <el-input v-model="dataForm.summary" placeholder="资源简介"></el-input>
    </el-form-item>
    <el-form-item label="资源介绍" prop="content">
      <el-input v-model="dataForm.content" placeholder="资源介绍"></el-input>
    </el-form-item>
    <el-form-item label="资源图片uid" prop="fileUid">
      <el-input v-model="dataForm.fileUid" placeholder="资源图片uid"></el-input>
    </el-form-item>
    <el-form-item label="资源分类: 1:电子书 2: Office文档 3:压缩包 4:云盘资源 5:MAC专区 6:Windows专区" prop="resourceSort">
      <el-input v-model="dataForm.resourceSort" placeholder="资源分类: 1:电子书 2: Office文档 3:压缩包 4:云盘资源 5:MAC专区 6:Windows专区"></el-input>
    </el-form-item>
    <el-form-item label="云盘链接" prop="panPath">
      <el-input v-model="dataForm.panPath" placeholder="云盘链接"></el-input>
    </el-form-item>
    <el-form-item label="云盘Code" prop="panCode">
      <el-input v-model="dataForm.panCode" placeholder="云盘Code"></el-input>
    </el-form-item>
    <el-form-item label="点击数" prop="clickCount">
      <el-input v-model="dataForm.clickCount" placeholder="点击数"></el-input>
    </el-form-item>
    <el-form-item label="收藏点击数" prop="collectCount">
      <el-input v-model="dataForm.collectCount" placeholder="收藏点击数"></el-input>
    </el-form-item>
    <el-form-item label="点赞数" prop="praiseCount">
      <el-input v-model="dataForm.praiseCount" placeholder="点赞数"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="折扣价格" prop="discountPrice">
      <el-input v-model="dataForm.discountPrice" placeholder="折扣价格"></el-input>
    </el-form-item>
    <el-form-item label="收费模式 1:免费 2:一口价 3:折扣价 4:会员免费" prop="chargeType">
      <el-input v-model="dataForm.chargeType" placeholder="收费模式 1:免费 2:一口价 3:折扣价 4:会员免费"></el-input>
    </el-form-item>
    <el-form-item label="支付类型：1:积分支付，2: 现金支付" prop="payType">
      <el-input v-model="dataForm.payType" placeholder="支付类型：1:积分支付，2: 现金支付"></el-input>
    </el-form-item>
    <el-form-item label="折扣开始时间" prop="discountStartTime">
      <el-input v-model="dataForm.discountStartTime" placeholder="折扣开始时间"></el-input>
    </el-form-item>
    <el-form-item label="折扣结束时间" prop="discountEndTime">
      <el-input v-model="dataForm.discountEndTime" placeholder="折扣结束时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="排序字段" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段"></el-input>
    </el-form-item>
    <el-form-item label="1: 阿里网盘，2:百度网盘" prop="panType">
      <el-input v-model="dataForm.panType" placeholder="1: 阿里网盘，2:百度网盘"></el-input>
    </el-form-item>
    <el-form-item label="用户uid" prop="userUid">
      <el-input v-model="dataForm.userUid" placeholder="用户uid"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          name: '',
          summary: '',
          content: '',
          fileUid: '',
          resourceSort: '',
          panPath: '',
          panCode: '',
          clickCount: '',
          collectCount: '',
          praiseCount: '',
          price: '',
          discountPrice: '',
          chargeType: '',
          payType: '',
          discountStartTime: '',
          discountEndTime: '',
          status: '',
          createTime: '',
          updateTime: '',
          sort: '',
          panType: '',
          userUid: '',
          isDelete: ''
        },
        dataRule: {
          name: [
            { required: true, message: '资源名称不能为空', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '资源简介不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '资源介绍不能为空', trigger: 'blur' }
          ],
          fileUid: [
            { required: true, message: '资源图片uid不能为空', trigger: 'blur' }
          ],
          resourceSort: [
            { required: true, message: '资源分类: 1:电子书 2: Office文档 3:压缩包 4:云盘资源 5:MAC专区 6:Windows专区不能为空', trigger: 'blur' }
          ],
          panPath: [
            { required: true, message: '云盘链接不能为空', trigger: 'blur' }
          ],
          panCode: [
            { required: true, message: '云盘Code不能为空', trigger: 'blur' }
          ],
          clickCount: [
            { required: true, message: '点击数不能为空', trigger: 'blur' }
          ],
          collectCount: [
            { required: true, message: '收藏点击数不能为空', trigger: 'blur' }
          ],
          praiseCount: [
            { required: true, message: '点赞数不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          discountPrice: [
            { required: true, message: '折扣价格不能为空', trigger: 'blur' }
          ],
          chargeType: [
            { required: true, message: '收费模式 1:免费 2:一口价 3:折扣价 4:会员免费不能为空', trigger: 'blur' }
          ],
          payType: [
            { required: true, message: '支付类型：1:积分支付，2: 现金支付不能为空', trigger: 'blur' }
          ],
          discountStartTime: [
            { required: true, message: '折扣开始时间不能为空', trigger: 'blur' }
          ],
          discountEndTime: [
            { required: true, message: '折扣结束时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段不能为空', trigger: 'blur' }
          ],
          panType: [
            { required: true, message: '1: 阿里网盘，2:百度网盘不能为空', trigger: 'blur' }
          ],
          userUid: [
            { required: true, message: '用户uid不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/resource/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.resource.name
                this.dataForm.summary = data.resource.summary
                this.dataForm.content = data.resource.content
                this.dataForm.fileUid = data.resource.fileUid
                this.dataForm.resourceSort = data.resource.resourceSort
                this.dataForm.panPath = data.resource.panPath
                this.dataForm.panCode = data.resource.panCode
                this.dataForm.clickCount = data.resource.clickCount
                this.dataForm.collectCount = data.resource.collectCount
                this.dataForm.praiseCount = data.resource.praiseCount
                this.dataForm.price = data.resource.price
                this.dataForm.discountPrice = data.resource.discountPrice
                this.dataForm.chargeType = data.resource.chargeType
                this.dataForm.payType = data.resource.payType
                this.dataForm.discountStartTime = data.resource.discountStartTime
                this.dataForm.discountEndTime = data.resource.discountEndTime
                this.dataForm.status = data.resource.status
                this.dataForm.createTime = data.resource.createTime
                this.dataForm.updateTime = data.resource.updateTime
                this.dataForm.sort = data.resource.sort
                this.dataForm.panType = data.resource.panType
                this.dataForm.userUid = data.resource.userUid
                this.dataForm.isDelete = data.resource.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/resource/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'name': this.dataForm.name,
                'summary': this.dataForm.summary,
                'content': this.dataForm.content,
                'fileUid': this.dataForm.fileUid,
                'resourceSort': this.dataForm.resourceSort,
                'panPath': this.dataForm.panPath,
                'panCode': this.dataForm.panCode,
                'clickCount': this.dataForm.clickCount,
                'collectCount': this.dataForm.collectCount,
                'praiseCount': this.dataForm.praiseCount,
                'price': this.dataForm.price,
                'discountPrice': this.dataForm.discountPrice,
                'chargeType': this.dataForm.chargeType,
                'payType': this.dataForm.payType,
                'discountStartTime': this.dataForm.discountStartTime,
                'discountEndTime': this.dataForm.discountEndTime,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'sort': this.dataForm.sort,
                'panType': this.dataForm.panType,
                'userUid': this.dataForm.userUid,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
